//
//  ADImageSelectView.m
//  ADKit
//
//  Created by duzexu on 2017/12/28.
//

#import "ADImageSelectView.h"
#import "ADKitHeaders.h"

@interface ADImageSelectView ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (weak, nonatomic) ADViewModel *viewModel;

@end

@implementation ADImageSelectView

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self _initialize];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self _initialize];
        _allowsEditing = YES;
    }
    return self;
}

- (void)_initialize {
    self.contentMode = UIViewContentModeScaleAspectFill;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImage:)];
    [self setUserInteractionEnabled:YES];
    [self addGestureRecognizer:tap];
}

- (void)selectImage:(id)sender {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册中选择", @"用摄像头拍照", nil];
        [actionSheet showInView:self.viewModel.viewController.view];
    }else{
        [self actionSheet:[NSNull null] clickedButtonAtIndex:0];
    }
}

#pragma mark- actionSheet Delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:{
            //从相册中选择
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            imagePickerController.delegate = self;
            imagePickerController.allowsEditing = _allowsEditing;
            imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self.viewModel.viewController presentViewController:imagePickerController animated:YES completion:nil];
            break;
        }
        case 1:{
            //拍照获取
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
                imagePickerController.delegate = self;
                imagePickerController.allowsEditing = _allowsEditing;
                imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
                [self.viewModel.viewController presentViewController:imagePickerController animated:YES completion:nil];
            }
            break;
        }
        default:
            break;
    }
}

#pragma mark- UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *image;
    if (_allowsEditing) {
        image = info[UIImagePickerControllerEditedImage];
    }else{
        image = info[UIImagePickerControllerOriginalImage];
    }
    if (_saveAlbum && picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, NULL);
    }
    if ([_delegate respondsToSelector:@selector(view:didSelectWithImage:)]) {
        [_delegate view:self didSelectWithImage:image];
    }
//    self.image = image;
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Getter & Setter
- (ADViewModel *)viewModel {
    if ([_delegate isKindOfClass:[ADViewModel class]]) {
        _viewModel = (ADViewModel *)_delegate;
    }else if ([_delegate isKindOfClass:[ADViewController class]]){
        _viewModel = ((ADViewController *)_delegate).viewModel;
    }
    return _viewModel;
}

@end
